package com.iurac.service.impl;

import com.iurac.dao.DictionaryDao;
import com.iurac.domain.DicType;
import com.iurac.domain.DicValue;
import com.iurac.exception.ManageException;
import com.iurac.service.DictionaryService;
import com.iurac.vo.DictionaryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@Service("dictionaryService")
public class DictionaryServiceImpl implements DictionaryService {

    @Autowired
    DictionaryDao dictionaryDao;

    @Override
    @Transactional(readOnly = true)
    public Map<String, List<DicValue>> getDictionary() {
        Map<String, List<DicValue>> map = new HashMap<>();

        List<DicType> dicTypeList = dictionaryDao.getDicType();
        for(DicType type: dicTypeList){
            List<DicValue> dicValueList = dictionaryDao.getDicValueByTypeId(type.getId());

            map.put(type.getType(),dicValueList);
        }

        return map;
    }

    @Override
    @Transactional(readOnly = true)
    public List<DictionaryVo> getDictionaryByCondition() {
        return dictionaryDao.getDictionaryByCondition();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = ManageException.class)
    public boolean deleteDictionaryByVid(String valueId) throws ManageException {
        String typeId = dictionaryDao.getDicTypeIdByVid(valueId);
        int result1 = dictionaryDao.deleteDicValueById(valueId);
        if(result1==1){
            int valueCount = dictionaryDao.getDicValueCountByTypeId(typeId);
            if(valueCount==0){
                int result2 = dictionaryDao.deleteDicTypeById(typeId);
                if(result2!=1){
                    throw new ManageException("删除该字典值关联类型失败");
                }
            }
            return true;
        }
        throw new ManageException("删除字典值失败");
    }

    @Override
    @Transactional(readOnly = true)
    public List<DicType> getDicType() {
        return dictionaryDao.getDicType();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = ManageException.class)
    public boolean saveDicType(DicType dicType) throws ManageException {
        int result = dictionaryDao.saveDicType(dicType);
        if(result!=1){
            throw new ManageException("添加字典类型失败");
        }
        return true;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = ManageException.class)
    public boolean saveDicValue(DicValue dicValue) throws ManageException {
        String orderNo = dictionaryDao.getOrderNoByTypeId(dicValue.getTypeId());
        dicValue.setOrderNo(orderNo);
        int result = dictionaryDao.saveDicValue(dicValue);
        if(result!=1){
            throw new ManageException("添加字典值失败");
        }
        return true;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = ManageException.class)
    public boolean updateDicValue(DicValue dicValue) throws ManageException {
        String orderNo = dictionaryDao.getOrderNoByTypeId(dicValue.getTypeId());
        dicValue.setOrderNo(orderNo);
        int result = dictionaryDao.updateDicValue(dicValue);
        if(result!=1){
            throw new ManageException("更新字典值失败");
        }
        return true;
    }
}
